package com.example.my;


import android.os.Bundle;
import android.view.View;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.my.fragment.FragmentAdapter;
import com.example.my.fragment.Tab1Fragment;
import com.example.my.fragment.Tab2Fragment;
import com.example.my.fragment.Tab3Fragment;
import com.example.my.fragment.Tab4Fragment;

import java.util.ArrayList;
import java.util.List;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener{


    private ViewPager viewPager;
    private ImageView img1,img2,img3,img4;
    private TextView tv1,tv2,tv3,tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        viewPager = findViewById(R.id.view_pager);
        List<Fragment>list =new ArrayList<>();




        //TODO 页面滑动

        Tab1Fragment tab1Fragment =new Tab1Fragment();
        Tab2Fragment tab2Fragment =new Tab2Fragment();
        Tab3Fragment tab3Fragment =new Tab3Fragment();
        Tab4Fragment tab4Fragment =new Tab4Fragment();
        list.add(tab1Fragment);
        list.add(tab2Fragment);
        list.add(tab3Fragment);
        list.add(tab4Fragment);
        FragmentPagerAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),list);

        viewPager.setAdapter(adapter);

        LinearLayout tab1 = findViewById(R.id.tab_1);
        tab1.setOnClickListener(this);

        LinearLayout tab2 = findViewById(R.id.tab_2);
        tab2.setOnClickListener(this);

        LinearLayout tab3 = findViewById(R.id.tab_3);
        tab3.setOnClickListener(this);

        LinearLayout tab4 = findViewById(R.id.tab_4);
        tab4.setOnClickListener(this);

        img1 = findViewById(R.id.img_1);
        img2 = findViewById(R.id.img_2);
        img3 = findViewById(R.id.img_3);
        img4 = findViewById(R.id.img_4);

        tv1 = findViewById(R.id.tv_1);
        tv2 = findViewById(R.id.tv_2);
        tv3 = findViewById(R.id.tv_3);
        tv4 = findViewById(R.id.tv_4);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageScrollStateChanged(int state) {}

            @Override
            public void onPageSelected(int position) {
                setNormal();
                if(position == 0) {
                    img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                    tv1.setTextColor(getResources().getColor(R.color.blue));
                } else if(position == 1) {
                    img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                    tv2.setTextColor(getResources().getColor(R.color.blue));
                } else if(position == 2) {
                    img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                    tv3.setTextColor(getResources().getColor(R.color.blue));
                } else if(position == 3) {
                    img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                    tv4.setTextColor(getResources().getColor(R.color.blue));

                }
            }
        });

        img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
        tv1.setTextColor(getResources().getColor(R.color.blue));
    }
    @Override
    public void onClick(View v) {
        //把所有的 img tv 变成灰色，666666
        setNormal();
        switch (v.getId()) {
            case R.id.tab_1:
                //viewPager 选择第一个Fragment;
                viewPager.setCurrentItem(0);
                //img_1 tv_1 变成黄色
                img1.setBackgroundResource(R.drawable.home_page_tab1_selected);
                tv1.setTextColor(getResources().getColor(R.color.blue));
                break;
            case R.id.tab_2:
                viewPager.setCurrentItem(1);
                img2.setBackgroundResource(R.drawable.home_page_tab2_selected);
                tv2.setTextColor(getResources().getColor(R.color.blue));
                break;
            case R.id.tab_3:
                viewPager.setCurrentItem(2);
                img3.setBackgroundResource(R.drawable.home_page_tab3_selected);
                tv3.setTextColor(getResources().getColor(R.color.blue));
                break;
            case R.id.tab_4:
                viewPager.setCurrentItem(3);
                img4.setBackgroundResource(R.drawable.home_page_tab4_selected);
                tv4.setTextColor(getResources().getColor(R.color.blue));
                break;
        }
    }

    private void setNormal() {
        img1.setBackgroundResource(R.drawable.home_page_tab1_normal);
        img2.setBackgroundResource(R.drawable.home_page_tab2_normal);
        img3.setBackgroundResource(R.drawable.home_page_tab3_normal);
        img4.setBackgroundResource(R.drawable.home_page_tab4_normal);
        tv1.setTextColor(getResources().getColor(R.color.gary));
        tv2.setTextColor(getResources().getColor(R.color.gary));
        tv3.setTextColor(getResources().getColor(R.color.gary));
        tv4.setTextColor(getResources().getColor(R.color.gary));

    }

}
